﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

//ViewState
//1.	QuestionID

public partial class Admin_ExpertAnswerOffLineQuestion : System.Web.UI.Page
{
	/// <summary>
	/// 专家ID
	/// </summary>
	private int nExpertID = 0;
	/// <summary>
	/// 专家控制类
	/// </summary>
	private PX.Expert.ExpertComponent clsExpertComp = new PX.Expert.ExpertComponent();
	/// <summary>
	/// 配置类
	/// </summary>
	private PX.Config _clsConfig = new PX.Config();
	/// <summary>
	/// 需要处理的问题
	/// </summary>
	private List<PX.Question.Entity> listQuestions = new List<PX.Question.Entity>();
	/// <summary>
	/// 问题控制类
	/// </summary>
	private PX.Question.Component clsQuestionComp = new PX.Question.Component();

	/// <summary>
	/// 回答控制类
	/// </summary>
	private PX.Answer.Component clsAnswerComp = new PX.Answer.Component();

	/// <summary>
	/// GridView控件样式设定
	/// </summary>
	private SetControl.GridViewControl.A clsGridViewStyle;

	protected override void OnInit(EventArgs e)
	{
		base.OnInit(e);

		clsExpertComp.page = Page;
		_clsConfig.clsPage = Page;
		clsQuestionComp.page = Page;

		clsGridViewStyle = new SetControl.GridViewControl.A(GrvQuestion);

		GrvQuestion.RowCommand += new GridViewCommandEventHandler(GrvQuestion_RowCommand);
		GrvQuestion.RowDataBound += new GridViewRowEventHandler(GrvQuestion_RowDataBound);
		BtnAnswer.Click += new EventHandler(BtnAnswer_Click);
	}

	void BtnAnswer_Click(object sender, EventArgs e)
	{
		int nQuestionID = 0;
		PX.Question.Entity clsQuestion = new PX.Question.Entity();
		PX.Answer.Entity clsAnswer = new PX.Answer.Entity();

		try
		{
			nQuestionID = Convert.ToInt32(ViewState["QuestionID"]);
		}
		catch
		{
			nQuestionID = 0;
		}

		clsQuestion = clsQuestionComp.GetOne(nQuestionID);

		clsAnswer.AskerID = clsQuestion.AskerID;
		clsAnswer.ExpertID = nExpertID;
		clsAnswer.QuestionID = clsQuestion.ID;
		clsAnswer.Content = TbAnswer.Text;

		if (clsAnswerComp.Add(clsAnswer))
		{
			clsQuestion.nAnswerID = clsAnswerComp.GetNewestAddID();
			clsQuestion.HaveAnswer = _clsConfig.nHaveAnswer;

			if (clsQuestionComp.Modify(clsQuestion))
			{

			}
		}
	}

	void GrvQuestion_RowDataBound(object sender, GridViewRowEventArgs e)
	{
		
	}

	void GrvQuestion_RowCommand(object sender, GridViewCommandEventArgs e)
	{
		int nQuestionID;

		if (e.CommandName == "Answer")
		{
			nQuestionID = Convert.ToInt32(e.CommandArgument);

			ShowQuestionDetail(nQuestionID);

			ViewState["QuestionID"] = nQuestionID;
		}

	}

	protected void Page_Load(object sender, EventArgs e)
	{
		nExpertID = GetExpertID();

		

		if (!IsPostBack)
		{
			QueryQuestion();
			BindGridView();
		}
	}

	#region 自定义函数

	/// <summary>
	/// 获得专家ID
	/// </summary>
	/// <returns></returns>
	private int GetExpertID()
	{
		int nExpertUserID = 0;
		PX.Expert.Expert clsExpert = new PX.Expert.Expert();

		try
		{
			nExpertUserID = Convert.ToInt32(Session["LoginUserID"]);
		}
		catch
		{
			nExpertUserID = 0;
		}

		if (nExpertUserID == 0)
		{
			// 登录超时，返回登录页面

			_clsConfig.ErrorLoginGoLogin();
			return 0;
		}
		else
		{
			clsExpert = clsExpertComp.QueryOne(nExpertUserID);
			if (clsExpert != null)
			{
				return clsExpert.ID;
			}
			else
			{
				_clsConfig.ErrorLoginGoLogin();
				return 0;
			}
		}
	}

	/// <summary>
	/// 查询需要解决的问题
	/// </summary>
	private void QueryQuestion()
	{
		listQuestions = clsQuestionComp.QueryNoAnswerOffLine(nExpertID);
	}

	/// <summary>
	/// 显示一个问题具体内容
	/// </summary>
	private void ShowQuestionDetail(int nQuestionID)
	{
		PX.Question.Entity clsQuestion = new PX.Question.Entity();

		clsQuestion = clsQuestionComp.GetOne(nQuestionID);

		LabAskername.Text = clsQuestion.AskerName;
		TbQuestion.Text = clsQuestion.Content;
	}

	#endregion 自定义函数

	#region 控件绑定

	/// <summary>
	/// 绑定GridView控件
	/// </summary>
	private void BindGridView()
	{

		GrvQuestion.DataSource = listQuestions;
		GrvQuestion.DataKeyNames = new string[] { "ID" };
		GrvQuestion.DataBind();
	}

	#endregion 控件绑定
}
